package fem2d;

import java.io.IOException;

import fem2.Constraint;
import fem2.Debugger;
import fem2.MaterialModel;
import fem2.Mesh;
import fem2.MeshGenerator;
import fem2.MeshUtilities;
import fem2.Model;
import fem2.analysis.Analysis;
import fem2.analysis.LinearStaticAnalysis;
import fem2.element.StructuralElement;
import fem2.enu.EchoLevelType;
import fem2.enu.State;
import fem2.enu.StressType;
import fem2.material.StVenantKirchhoffMM;
import fem2.observer.StressObserver;
import fem2.pre_and_post.GidPostStructural;
import fem2.pre_and_post.GmshMeshGenerator;

/**
 * desmonstrate mixed triangle-quadrilateral capability
 * 
 * @author hbui
 * 
 */
public class LshapeMixed {

	static String projDir = "/home/hbui/kratos_janosch";
	static String projName = "Lshape.gid";
	static String meshFileName = "Lshape.gmsh.1";
	String fn = projDir + '/' + projName + '/' + meshFileName + ".msh";

	public Model createModel() {
		MeshGenerator mg = new GmshMeshGenerator(fn);
		Mesh mesh = mg.getMesh(2);

		Model m = new Model(mesh);

		mesh.setNumberOfFields(2);

		/*
		 * element creation
		 */
		double E = 25850;
		double nu = 0.18;
		double t = 200;
		State ss = State.PLANE_STRESS;
		MaterialModel mm = new StVenantKirchhoffMM(E, nu, t, 0, ss);
		for (int i = 0; i < mesh.countFaces(); i++) {
			StructuralElement e = new StructuralElement(mesh.getFace(i), mm);
			m.addElement(e);
		}

		// mesh.Print();

		/*
		 * conditions
		 */
		Constraint c1 = new Constraint(false, false);
		Constraint c2 = new Constraint(true, false);
		c2.setValue(1, -0.1);
		mesh.setConstraint(c1, MeshUtilities.seekNodesOnSurface(mesh, 0, 1, -1000));
		MeshUtilities.seekNode(mesh, 1000, 500).setConstraint(c2);

		return m;
	}

	public static void main(String[] args) throws IOException {
		Model m = new LshapeMixed().createModel();

		Analysis an = new LinearStaticAnalysis(m);
		// an.getStrategy().setSolverType(SolverType.UMFPACK_SOLVER);
		an.getStrategy().addObserver(new StressObserver(m));
		an.getStrategy().setEchoLevel(EchoLevelType.OUTPUT, EchoLevelType.AUX1);
		an.getModel().setEchoLevel(EchoLevelType.AUX1);
		an.run();

		/*
		 * post process
		 */
		// Mesh mesh = m.getMesh();
		// for (int i = 0; i < mesh.countNodes(); i++) {
		// System.out.println(ArrayFormat.format(mesh.getNode(i).getUHatLocal()));
		// }

		GidPostStructural post = new GidPostStructural(projDir, projName, meshFileName);
		post.clearAllPostFiles();
		// post.clearAllPostFiles(meshFileName);
		post.writeMesh(m, 0);
		post.writeNodalDisplacementResults(m, 0);
		post.writeGaussPointStressResults(m, 0, StressType.CAUCHY_STRESS);
		// post.moveAllPostFiles(meshFileName);

		Debugger.warn("Analysis completed");
	}
}
